//
//  selectAreaTabVC.m
//  Recycling
//
//  Created by jzd on 2018/6/2.
//  Copyright © 2018年 宋佳. All rights reserved.
//

#import "selectAreaTabVC.h"
#import "areaModel.h"
#import "areaCell.h"

@interface selectAreaTabVC ()
@property(strong,nonatomic)NSMutableArray *dataArray;
@end

@implementation selectAreaTabVC

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BGCOLOR;
    self.navigationItem.title = @"区域选择";
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([areaCell class]) bundle:nil] forCellReuseIdentifier:@"areaCell"];
    [self requestData];
}

-(void)requestData{
    NSDictionary *dict = @{@"uid":@([UserInfoTool shareUserInfoTool].Info.ID)};
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dict];
    [params setObject:[NSString currentTimeStr] forKey:@"timestamp"];
    [[SJNetWorkTool sharedTool]requestWithURLString:getAreaList parameters:params method:POST callBack:^(id responseObject, NSError *error) {
        if (responseObject) {
            int code = [[responseObject objectForKey:@"Code"] intValue];
            NSString *msg = [responseObject objectForKey:@"ErrorMessage"];
            id data = [responseObject objectForKey:@"Data"];
            if (code == 1) {
                if (![data isKindOfClass:[NSNull class]]) {
                    NSArray *arr = [areaModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"rows"]];
                    if (arr.count != 0) {
                        self.dataArray = arr.mutableCopy;
                    }else{
                        [self.navigationController.view showToast:@"暂无数据" completion:nil];
                    }
                    
                }else{
                    [self.navigationController.view showToast:@"暂无数据" completion:nil];
                }
            }else{
                [self.navigationController.view showToast:msg completion:nil];
            }
        }
        [self.tableView reloadData];
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    areaModel *model = [self.dataArray objectAtIndex:indexPath.row];
    areaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"areaCell"];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    areaModel *model = [self.dataArray objectAtIndex:indexPath.row];
    if (model.state ==1) {
        return;
    }
    if (self.resultBlock) {
        self.resultBlock(model);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
@end
